import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置 NProgress
NProgress.configure({ 
  showSpinner: false,
  minimum: 0.1,
  easing: 'ease',
  speed: 500
})

export function setupProgress(router) {
  router.beforeEach((to, from, next) => {
    if (to.path !== from.path) {
      NProgress.start()
    }
    next()
  })

  router.afterEach(() => {
    NProgress.done()
  })

  // 配置全局的 axios 请求进度条
  let requestCount = 0

  function handleRequest() {
    if (requestCount === 0) {
      NProgress.start()
    }
    requestCount++
  }

  function handleResponse() {
    requestCount--
    if (requestCount === 0) {
      NProgress.done()
    }
  }

  return {
    handleRequest,
    handleResponse
  }
}
